<template>
	<view class="ask_box">
		<view>
			<image class="box_img" :src="img_src + 'sqdl.png'" mode=""></image>
		</view>
		<view class="text-center margin-top">
			<button class="cu-btn block radius margin-tb-sm lg flex-sub margin-xs box_btn text-white" open-type="getUserInfo"
			 @getuserinfo="user_login">授权登录</button>
			<text class="margin-top text-orange" style="display: block;">杭爱在线小程序仅限全国持证残疾人使用</text>
			<!-- <image class="box_logo margin-bottom-sm" :src="img_src + 'hzcl.png'" mode=""></image> -->
		</view>
	</view>
</template>
<script>
	import {
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				img_src: this.$utils.SYS_URL + this.$utils.IMG_URL,
				session: {
					code: null,
					skey: null
				},
				users: {
					state: '0'
				},
			}
		},
		computed: {

		},
		onShow() {
			this.code_login()
		},
		methods: {
			...mapMutations(['login']),
			code_login() {
				uni.login({
					provider: 'weixin',
					success: (code_res => {
						this.session.code = code_res.code;
						this.session.skey = code_res.session_key;
					}),
					fail: () => {
						uni.showToast({
							title: "授权失败",
							icon: "none"
						});
					}
				});
			},
			//2. 页面获取用户信息并登录
			user_login(it) {
				// #ifdef MP-WEIXIN || MP-QQ
				if (!it.detail.userInfo) {
					uni.showToast({
						title: "授权失败",
						icon: "none"
					});
					return;
				}
				// 通过前台获取微信用户信息
				uni.showLoading({
					title: "正在进入中"
				});
				this.users.nickName = it.detail.userInfo.nickName;
				this.users.avatar = it.detail.userInfo.avatarUrl;
				this.users.openTy = 'MP-WEIXIN';

				var authData = {
					"js_code": this.session.code,
					"encrypt_data": it.detail.encryptedData,
					"iv": it.detail.iv
				};
				this.sure_login('MP-WEIXIN', JSON.stringify(authData));
				// ./通过前台获取微信用户信息
				// #endif

				// #ifdef APP-PLUS || H5

				// #endif
			},
			//3. 后台获取用户信息并登录
			sure_login(authType, authData) {
				let _this = this;
				var params = {
					"authType": authType,
					"authData": authData
				};
				this.$request.postForm('/view/users/login', params).then(
					res => {
						if(res.data) {
							_this.users.state = res.data.users.state;
							_this.users.citizenName = res.data.users.citizenName;
							_this.users.uid = res.data.users.uid;
							_this.users.citizenId = res.data.users.citizenId;
							_this.users.stateId = res.data.users.stateId;
							_this.users.phone = res.data.users.phone;
							_this.users.citizenType = res.data.users.citizenType;
							_this.users.openId = JSON.parse(res.data.users.json).openId;
							_this.users.unionId = JSON.parse(res.data.users.json).unionId;
							_this.$store.commit('login', _this.users);
							uni.setStorageSync('token', res.data.token);
							uni.hideLoading();
							
							if(_this.users.stateId == '1'){
								uni.switchTab({
									url: '/pages/admin/vhome',
									success: (res) => {
										let page = getCurrentPages().pop(); //当前页面
										if (page == undefined || page == null) return;
										page.onLoad();
									}
								});
							} else {
								uni.navigateTo({url: '/pages_mine/users/regit'});
							}
							
							// uni.showModal({
							// 	title: '提示',
							// 	content: '授权成功，是否继续实名认证',
							// 	success: (e) => {
							// 		if (e.confirm) {
										
							// 		} else {
							// 			uni.switchTab({
							// 				url: '/pages/admin/vhome',
							// 				success: (res) => {
							// 					let page = getCurrentPages().pop(); //当前页面
							// 					if (page == undefined || page == null) return;
							// 					page.onLoad();
							// 				}
							// 			});
							// 		}
							// 	}
							// });
						}
					},
					err => {
						console.log(err);
					}
				);
			},
		}
	}
</script>

<style>
	page {
		background: #fff;
	}

	.box_img {
		width: 750rpx;
		height: 980rpx;
	}

	.box_btn {
		background-image: linear-gradient(#ea5815, #f39a02);
		border-radius: 99px;
		margin: 0 50rpx;
	}

	.box_logo {
		width: 500rpx;
		height: 64rpx;
	}
</style>
